package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.config.goods.GoodsBagInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.bussiness.config.GoodsBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.travelbook.TravelBookModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.*;

public class GoodsBagMgr extends TempMgr {

	private static Map<Integer, Map<Integer, List<GoodsBagInfo>>> goodsBagMap;

	private static RandomHelper random = new RandomHelper();

	@Override
	public boolean reloadConfig() {
		goodsBagMap = GoodsBussiness.getGoodsBagInfoMap();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return true;
	}

	@Override
	public boolean save() {
		return false;
	}

	/**
	 * 获取宝箱内所有道具信息
	 */
	public static List<GoodsBagInfo> getGoodsBagInfoList(int bagId) {
		List<GoodsBagInfo> goodsBagInfoList = new ArrayList<>();
		if (goodsBagMap.containsKey(bagId)) {
			for (List<GoodsBagInfo> infoList : goodsBagMap.get(bagId).values()) {
				goodsBagInfoList.addAll(infoList);
			}
		}
		return goodsBagInfoList;
	}

	/**
	 * 获取礼包里面随机道具
	 */
	public static OpenGoodsBagResult getRandomGoods(int bagId) {
		List<GoodsBagInfo> goodsList = getRandomGoodsList(bagId);
		if(null == goodsList){
			goodsList = CommonMgr.getRandomGoodsList(bagId);
		}
		return getRandomGoods(goodsList);
	}

	/**
	 * 获取礼包内所有道具
	 */
	public static OpenGoodsBagResult getAllGoods(int bagId) {
		List<GoodsBagInfo> goodsList = getGoodsBagInfoList(bagId);
		if(goodsList.size() == 0){
			goodsList = CommonMgr.getGoodsBagInfoList(bagId);
		}
		return getRandomGoods(goodsList);
	}


	private static OpenGoodsBagResult getRandomGoods(List<GoodsBagInfo> goodsBagInfoList) {
		OpenGoodsBagResult result = new OpenGoodsBagResult();
		if(null == goodsBagInfoList) {
			return result;
		}
		Property reward = new Property();
		for(GoodsBagInfo goodsBagInfo : goodsBagInfoList) {
			if (result.getNotice() == 0) {
				result.setNotice(goodsBagInfo.getNotice());
			}
			reward.addProperty(goodsBagInfo.getGoodsId(), BigInteger.valueOf(random.next(goodsBagInfo.getMinNum(), goodsBagInfo.getMaxNum() + 1)));
		}
		result.setReward(reward);
		return result;
	}

	/**
	 * 获取礼包里面随机道具信息列表
	 */
	private static List<GoodsBagInfo> getRandomGoodsList(int bagId) {
		List<GoodsBagInfo> resultGoodsList = new ArrayList<>();
		Map<Integer, List<GoodsBagInfo>> goodsMap = goodsBagMap.get(bagId);
		if(null == goodsMap) {
			return null;
		}
		for(List<GoodsBagInfo> goodsList : goodsMap.values()){
			int sumWeight = 0;
			for(GoodsBagInfo goodsBagInfo : goodsList){
				sumWeight += goodsBagInfo.getRandomWeight();
			}
			if(sumWeight > 0){
				int randomWeight = random.next(0, sumWeight);
				int tempWeight = 0;
				for(GoodsBagInfo goodsBagInfo : goodsList){
					tempWeight += goodsBagInfo.getRandomWeight();
					if(tempWeight > randomWeight){
						resultGoodsList.add(goodsBagInfo);
						break;
					}
				}
			}
		}
		return resultGoodsList;
	}

	/**
	 * 判断是否 是宝箱道具
	 * @param goodId
	 * @return
	 */
	public static boolean isGoodsBag(int goodId){
		if (goodsBagMap.containsKey(goodId)) {
			return true;
		}
		if(CommonMgr.getGoodsBagInfoMap().containsKey(goodId)){
			return true;
		}
		return false;
	}

	/**
	 * 获取特殊礼包（type = 17），获取皮肤ID（有该门客才能获取到）
	 * @param player
	 * @param goodsInfo
	 * @return
	 */
	public static OpenGoodsBagResult openSpecialPatronsSkinBag(GamePlayer player, GoodsInfo goodsInfo) {
		if (goodsInfo.getType() != eGoodsType.SpecialPatronsSkinBox.getValue()) {
			return new OpenGoodsBagResult();
		}
		//获取礼包里的道具
		List<GoodsBagInfo> goodsList = new ArrayList<>();
		List<GoodsBagInfo> goodsBagInfoList =  CommonMgr.getGoodsBagInfoList(goodsInfo.getGoodsId());
		for (GoodsBagInfo goodsBagInfo : goodsBagInfoList) {
			//皮肤配置
			SkinInfo skinInfo = SkinMgr.getSkinInfo(goodsBagInfo.getGoodsId());
			if (skinInfo == null) {
				continue;
			}
			//获取皮肤ID，对应的玩家门客
			int patronsId = skinInfo.getBelongRole();
			UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
			if (userPatrons != null) {
				//添加皮肤
				goodsList.add(goodsBagInfo);
				break;
			}
		}
		if (goodsList.isEmpty()) {
			return new OpenGoodsBagResult();
		}
		//获取礼包里的道具
		return getRandomGoods(goodsList);
	}

	/**
	 * 获取开启结果
	 *
	 * @param bagId  盒子ID
	 * @param player 玩家
	 * @param hasGet 已经获得的道具
	 * @return 开启结果
	 */
	public static OpenGoodsBagResult getRandomGoods(int bagId, GamePlayer player, Property hasGet) {
		List<GoodsBagInfo> goodsList = getRandomGoodsList(bagId, player, hasGet);
		if (null == goodsList) {
			goodsList = CommonMgr.getRandomGoodsList(bagId, player, hasGet);
		}
		return getRandomGoods(goodsList);
	}

	/**
	 * 获取礼包里面随机道具，剔除部分无须随机的道具
	 *
	 * @param bagId 盒子ID
	 * @param player 玩家
	 * @param hasGet 已经获得的道具
	 * @return
	 */
	private static List<GoodsBagInfo> getRandomGoodsList(int bagId, GamePlayer player, Property hasGet) {
		List<GoodsBagInfo> resultGoodsList = new ArrayList<>();
		Map<Integer, List<GoodsBagInfo>> goodsMap = goodsBagMap.get(bagId);
		if(null == goodsMap) {
			return null;
		}
		for(List<GoodsBagInfo> goodsList : goodsMap.values()){
			int sumWeight = 0;
			Set<Integer> invalidGoods = new HashSet<>(); // 不用参与随机的道具
			for(GoodsBagInfo goodsBagInfo : goodsList){
				int goodsId = goodsBagInfo.getGoodsId();
				if (GoodsMgr.isTravelCard(goodsId) && player.getModule(TravelBookModule.class).checkCardGetMaxLevel(goodsId, hasGet)) {
					invalidGoods.add(goodsId);
					continue;
				}
				sumWeight += goodsBagInfo.getRandomWeight();
			}
			if(sumWeight > 0){
				int randomWeight = random.next(0, sumWeight);
				int tempWeight = 0;
				for(GoodsBagInfo goodsBagInfo : goodsList){
					if (invalidGoods.contains(goodsBagInfo.getGoodsId())) {
						continue;
					}
					tempWeight += goodsBagInfo.getRandomWeight();
					if(tempWeight > randomWeight){
						resultGoodsList.add(goodsBagInfo);
						break;
					}
				}
			}
		}
		return resultGoodsList;
	}
}
